<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="科室名称" prop="departmentName">
        <el-input v-model="dataForm.departmentName" placeholder="科室名称"></el-input>
      </el-form-item>
      <el-form-item label="所属医院" prop="hospitalId">
        <template slot-scope="scope">
          <el-select v-model="dataForm.hospitalId" v-bind:disabled="dataForm.isHospitalManager==1" filterable placeholder="请选择">
            <el-option
              v-for="item in hospitalOptions"
              :key="item.hospitalId"
              :label="item.hospitalName"
              :value="item.hospitalId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="科室备注" prop="departmentRemark">
        <el-input v-model="dataForm.departmentRemark" placeholder="科室备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                //医院列表
                hospitalOptions:[],
                visible: false,
                dataForm: {
                    departmentId: 0,
                    departmentName: '',
                    departmentRemark: '',
                    hospitalId: this.$cookie.get('hospitalId')!="null"?parseInt(this.$cookie.get('hospitalId')):'',
                    // isHospitalManager: 0,
                    isHospitalManager: this.$cookie.get('isHospitalManager'),
                },
                dataRule: {
                    departmentName: [
                        {required: true, message: '科室名称不能为空', trigger: 'blur'}
                    ],
                    hospitalId: [
                        {required: true, message: '所属医院不能为空', trigger: 'blur'}
                    ],
                }
            }
        },
        methods: {
            init(id) {
                //加载医院列表
                this.$http({
                    url: this.$http.adornUrl(`/bs/hospital/listWithNoPage`),
                    method: 'get'
                }).then(({data}) => {
                    if (data && data.code === 0) {
                        this.hospitalOptions = data.list;
                    }
                })
                //其他数据
                this.dataForm.departmentId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.departmentId) {
                        this.$http({
                            url: this.$http.adornUrl(`/bs/department/info/${this.dataForm.departmentId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm.departmentName = data.department.departmentName
                                this.dataForm.departmentRemark = data.department.departmentRemark
                                this.dataForm.hospitalId = data.department.hospitalId
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/bs/department/${!this.dataForm.departmentId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData({
                                'departmentId': this.dataForm.departmentId || undefined,
                                'departmentName': this.dataForm.departmentName,
                                'departmentRemark': this.dataForm.departmentRemark,
                                'hospitalId' : this.dataForm.hospitalId
                            })
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
